package no.uib.hplims.views;

import no.uib.hplims.views.auth.LoginScreen;
import org.vaadin.appfoundation.authentication.SessionHandler;
import org.vaadin.appfoundation.authentication.data.User;
import org.vaadin.appfoundation.view.View;
import org.vaadin.appfoundation.view.ViewContainer;
import org.vaadin.appfoundation.view.ViewHandler;

import com.vaadin.ui.Component;
import com.vaadin.ui.VerticalLayout;

public class MainWindow extends VerticalLayout implements ViewContainer {

	private static final long serialVersionUID = 1490961570483515444L;

	private VerticalLayout layout = new VerticalLayout();

	private View currentView = null;

	public MainWindow() {
		registerViews();

		buildMainLayout();
	}

	private void registerViews() {
		ViewHandler.addView(LoginScreen.class, this);
		ViewHandler.addView(MenuTabSheet.class, this);
	}

	private void buildMainLayout() {
		layout.setMargin(false, true, true, true);
		layout.setSizeFull();
		
		VerticalLayout spacer = new VerticalLayout();
		spacer.setHeight("12px");
		layout.addComponent(spacer);

		// Get currently logged in user
		User user = SessionHandler.get();
		if (user == null) {
			// Show login screen
			currentView = ViewHandler.getViewItem(LoginScreen.class).getView();
			layout.addComponent((Component) currentView);
		} else {
			currentView = ViewHandler.getViewItem(MenuTabSheet.class).getView();
			layout.addComponent((Component) currentView);
		}
		
		this.addComponent(layout);
	}

	public void activate(View view) {
		if (!(view instanceof Component)) {
			throw new IllegalArgumentException("View must be a component");
		}

		// Activate the view by replacing the current view with the given view
		// in the main layout
		layout.replaceComponent((Component) currentView, (Component) view);
		currentView = view;
	}

	public void deactivate(View view) {
		// TODO Auto-generated method stub

	}
}
